import { AfterViewInit, ChangeDetectorRef, Component, ElementRef, HostListener, NgZone, OnInit, QueryList, ViewChildren, ViewContainerRef } from '@angular/core';
import { NzContextMenuService, NzDropdownMenuComponent } from 'ng-zorro-antd/dropdown';
import { NzModalRef, NzModalService, NzModalState } from 'ng-zorro-antd/modal';
import { Host, HostConfigService, Setting, SettingService, WinForwardConfigService } from './service/config.service';
import { ElectronService } from './service/electron.service';
import { NotifyService } from './service/notify.service';
import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
import { ThemeService } from './service/theme.service';
import { LogService } from './service/log.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [NotifyService]
})
export class AppComponent implements OnInit {
  isLoading = true;

  constructor(private modal: NzModalService,
    public electron: ElectronService,
    private cd: ChangeDetectorRef,
    private nzContexMenuService: NzContextMenuService,
    private notify: NotifyService,
    private hostConfigService: HostConfigService,
    private settingService: SettingService,
    private themeService: ThemeService,
    private logger: LogService,
    private zone: NgZone,
    private forwardService: WinForwardConfigService) {
    logger.info(JSON.stringify('load host config...'))
    hostConfigService.loadConfig()
      .then(() => {
        logger.info(JSON.stringify('load setting...'))
        return settingService.loadConfig();
      })
      .then(() => {
        logger.info(JSON.stringify('load forwarding config...'))
        return forwardService.loadConfig();
      })
      .then(() => {
        logger.info(JSON.stringify('load theme...'))
        let setting = settingService.getConfig();
        logger.info(JSON.stringify(setting, null, 4));
        themeService.setThemeMode(setting.global.darkMode);
        return themeService.loadTheme();
      }).then(() => {
        this.isLoading = false;
      });
  }
  ngOnInit() {
    if (!this.electron.isElectron) {
      window.onbeforeunload = (e) => {
        (e || window.event).returnValue = 'confirm close ngTerm?';
      }
    }
  }
}
